package com.uenpay.im.ui.old.activity.movement;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.ui.old.activity.dynamic.Reply;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-21 下午1:27:26
 * @copyRight http://www.qiyunxin.com
 * @function 活动管理
 */
public class MovementHandler {

	public interface IResultListener {
		void onResult(int status, String msg);
	};

	/**
	 * 发布活动
	 * 
	 * @param movementId
	 * @param title
	 * @param starttime
	 * @param endtime
	 * @param personnum
	 * @param address
	 * @param fee
	 * @param description
	 * @param fileid
	 * @param filehash
	 * @param mResultListener
	 */
	public void publishMovement(int movementId, String title, String starttime,
			String endtime, String personnum, String address, String fee,
			String description, String fileid, String filehash,
			final IResultListener mResultListener) {
		PublishMovementInvokeItem item = new PublishMovementInvokeItem(
				movementId, title, starttime, endtime, personnum, address, fee,
				description, fileid, filehash);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							PublishMovementInvokeItem.PublishMovementInvokeItemResult result = ((PublishMovementInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mResultListener != null) {
									mResultListener.onResult(result.status,
											result.msg);
								}
							} else {
								if (mResultListener != null) {
									mResultListener.onResult(-1, "");
								}
							}
						} else {
							if (mResultListener != null) {
								mResultListener.onResult(-1, failReason);
							}
						}
					}
				});
	}

	public interface IGetMovementListResultListener {
		void onResult(int status, String msg,
							   ArrayList<MovementEntity> arrayList);
	};

	/**
	 * 获取活动列表
	 * 
	 * @param type
	 *            1：所有活动2：我参加的活动3：我创建的活动
	 * @param page
	 * @param cust_id
	 * @param movementListResultListener
	 */
	public void getMovementList(int type, int page, String cust_id,
			final IGetMovementListResultListener movementListResultListener) {
		GetMovementListInvokeItem item = new GetMovementListInvokeItem(type,
				page, cust_id);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetMovementListInvokeItem.GetMovementListInvokeItemResult result = ((GetMovementListInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (movementListResultListener != null) {
									movementListResultListener.onResult(
											result.status, result.msg,
											result.arrayList);
								}
							} else {
								if (movementListResultListener != null) {
									movementListResultListener.onResult(-1, "",
											null);
								}
							}
						} else {
							if (movementListResultListener != null) {
								movementListResultListener.onResult(-1, "",
										null);
							}
						}
					}
				});
	}

	public interface IMovementMembersListener {
		void onMembersResultListener(int status, String msg,
											  ArrayList<QYXUserEntity> arrayList);
	}

	/**
	 * 获取活动成员列表
	 * 
	 * @param page
	 * @param activityid
	 * @param mIMovementMembersListener
	 */
	public void getMovementMembersList(int page, int activityid,
			final IMovementMembersListener mIMovementMembersListener) {
		GetMovementMembersListInvokeItem item = new GetMovementMembersListInvokeItem(
				page, activityid);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetMovementMembersListInvokeItem.GetMovementMembersListInvokeItemResult result = ((GetMovementMembersListInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mIMovementMembersListener != null) {
									mIMovementMembersListener
											.onMembersResultListener(
													result.status, result.msg,
													result.arrayList);
								}
							} else {
								if (mIMovementMembersListener != null) {
									mIMovementMembersListener
											.onMembersResultListener(-1,
													failReason, null);
								}
							}
						} else {
							if (mIMovementMembersListener != null) {
								mIMovementMembersListener
										.onMembersResultListener(-1,
												failReason, null);
							}
						}
					}
				});
	}

	public interface IGetMovementDetialResult {
		void onMovementDetailResult(int status, String msg,
											 MovementEntity movementEntity);
	};

	/**
	 * 获取活动详情
	 * 
	 * @param movementId
	 * @param getMovementDetialResult
	 */
	public void getMovementDetial(int movementId,
			final IGetMovementDetialResult getMovementDetialResult) {
		GetMovementDetailInvokeItem item = new GetMovementDetailInvokeItem(
				movementId);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetMovementDetailInvokeItem.GetMovementDetailInvokeItemResult result = ((GetMovementDetailInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (getMovementDetialResult != null) {
									getMovementDetialResult
											.onMovementDetailResult(
													result.status, result.msg,
													result.movementEntity);
								}
							} else {
								if (getMovementDetialResult != null) {
									getMovementDetialResult
											.onMovementDetailResult(-1,
													failReason, null);
								}
							}
						} else {
							if (getMovementDetialResult != null) {
								getMovementDetialResult.onMovementDetailResult(
										-1, failReason, null);
							}
						}
					}
				});
	}

	/**
	 * 赞活动
	 * 
	 * @param movementId
	 * @param op
	 * @param mResultListener
	 */
	public void praiseMovement(int movementId, int op,
			final IResultListener mResultListener) {
		PraiseMovementInvokeItem item = new PraiseMovementInvokeItem(
				movementId, op);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							PraiseMovementInvokeItem.PraiseMovementInvokeItemResult result = ((PraiseMovementInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (mResultListener != null) {
									mResultListener.onResult(result.status,
											result.msg);
								}
							} else {
								if (mResultListener != null) {
									mResultListener.onResult(-1, failReason);
								}
							}
						} else {
							if (mResultListener != null) {
								mResultListener.onResult(-1, failReason);
							}
						}
					}
				});

	}

	public interface IReplyMovementResultListener {
		void onReplyMovementResult(int status, String msg, Reply mReply);
	};

	/**
	 * 评论活动
	 * 
	 * @param movementId
	 * @param content
	 * @param pCustId
	 * @param mResultListener
	 */
	public void replyMovement(int movementId, String content, String pCustId,
			final IReplyMovementResultListener mResultListener) {
		ReplyMovementInvokeItem item = new ReplyMovementInvokeItem(movementId,
				content, pCustId);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							ReplyMovementInvokeItem.ReplyMovementInvokeItemResult result = ((ReplyMovementInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (mResultListener != null) {
									mResultListener.onReplyMovementResult(
											result.status, result.msg,
											result.reply);
								}
							} else {
								if (mResultListener != null) {
									mResultListener.onReplyMovementResult(-1,
											"", null);
								}
							}
						} else {
							if (mResultListener != null) {
								mResultListener.onReplyMovementResult(-1, "",
										null);
							}
						}
					}
				});

	}

	/**
	 * 删除活动评论
	 * 
	 * @param replyid
	 * @param mResultListener
	 */
	public void deleteReplyMovement(String replyid,
			final IResultListener mResultListener) {
		DeleteMovementReplyInvokeItem item = new DeleteMovementReplyInvokeItem(
				replyid);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							DeleteMovementReplyInvokeItem.DeleteMovementReplyInvokeItemResult result = ((DeleteMovementReplyInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (mResultListener != null) {
									mResultListener.onResult(result.status,
											result.msg);
								}
							} else {
								if (mResultListener != null) {
									mResultListener.onResult(-1, "");
								}
							}
						} else {
							if (mResultListener != null) {
								mResultListener.onResult(-1, "");
							}
						}
					}
				});
	}

	/**
	 * 参加活动
	 * 
	 * @param movementId
	 * @param mResultListener
	 */
	public void joinMovement(int movementId,
			final IResultListener mResultListener) {
		JoinMovementInvokeItem item = new JoinMovementInvokeItem(movementId);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							JoinMovementInvokeItem.JoinMovementInvokeItemResult result = ((JoinMovementInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (mResultListener != null) {
									mResultListener.onResult(result.status,
											result.msg);
								}
							} else {
								if (mResultListener != null) {
									mResultListener.onResult(-1, failReason);
								}
							}
						} else {
							if (mResultListener != null) {
								mResultListener.onResult(-1, failReason);
							}
						}
					}
				});
	}

	/**
	 * 
	 * @param movementId
	 * @param mResultListener
	 */
	public void signMovement(int movementId,
			final IMovementResultListener mResultListener) {
		SignMovementInvokeItem item = new SignMovementInvokeItem(movementId);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							SignMovementInvokeItem.SignMovementInvokeItemResult result = ((SignMovementInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (mResultListener != null) {
									mResultListener.onResult(result.status,
											result.msg);
								}
							} else {
								if (mResultListener != null) {
									mResultListener.onResult(-1, "");
								}
							}
						} else {
							if (mResultListener != null) {
								mResultListener.onResult(-1, failReason);
							}
						}
					}
				});
	}

	/**
	 * 关闭或打开活动
	 * 
	 * @param movementId
	 * @param status
	 * @param mResultListener
	 */
	public void closeOrOpenMovement(int movementId, int status,
			final IMovementResultListener mResultListener) {
		CloseOrOpenMovementInvokeItem item = new CloseOrOpenMovementInvokeItem(
				movementId, status);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							CloseOrOpenMovementInvokeItem.CloseOrOpenMovementInvokeItemResult result = ((CloseOrOpenMovementInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (mResultListener != null) {
									mResultListener.onResult(result.status,
											result.msg);
								}
							} else {
								if (mResultListener != null) {
									mResultListener.onResult(-1, "");
								}
							}
						} else {
							if (mResultListener != null) {
								mResultListener.onResult(-1, failReason);
							}
						}
					}
				});
	}
}
